package com.tg.service.eventbus2;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.tg.service.eventbus2.component.CheckGroupCp;
import com.tg.service.eventbus2.component.FirstLoginCp;
import com.tg.service.eventbus2.entity.Event;
import com.tg.service.eventbus2.service.UserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

public class JavaStarter {

    private static final List<String> EVENTS = Lists.newArrayList("pay", "login");

    private static final Map<String, Supplier<List<Component>>> EVENT_2_CP = new HashMap<>();

    static {
        EVENT_2_CP.put("pay", () -> Lists.newArrayList(
                new FirstLoginCp(new UserService())
        ));
        EVENT_2_CP.put("login", () -> Lists.newArrayList(
                new FirstLoginCp(new UserService()),
                new CheckGroupCp()
        ));
    }

    private static final Map<String, ComponentBus> HANDLERS = Maps.newHashMap();

    public static void main(String[] args) {
        init();

        Event event = new Event();
        ComponentBus login = HANDLERS.get("login");
        login.post(event);
    }

    private static void init() {
        for (String event : EVENTS) {
            Supplier<List<Component>> supplier = EVENT_2_CP.get(event);
            ComponentBus handler = ComponentBus.of()
                    .setEventName(event)
                    .setComponents(supplier.get())
                    .init();

            HANDLERS.put(event, handler);
        }
    }
}
